package cn.com.ddapp.ddm.ui.mine;

import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.jungly.gridpasswordview.GridPasswordView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import butterknife.BindView;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.Entity;
import cn.com.ddapp.ddm.bean.mine.UserData;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.SPUtils;

//设置支付密码
public class SettingPwdAgainActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.gpv_setting_again)
    GridPasswordView gpvPassword;
    @BindView(R.id.error_layout)
    EmptyLayout mEmptyLayout;


    String email,phone,userId,userName,name;
    String pwd;
    UserData userData;
    OkUtilCallback callback1;

    @Override
    public void initView() {
        titletext.setText(R.string.setting_payPwd);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        gpvPassword.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onTextChanged(String psw) {

            }

            @Override
            public void onInputFinish(String psw) {
                if (!NetUtils.isConnected(SettingPwdAgainActivity.this)) {
                    Toast.makeText(SettingPwdAgainActivity.this, "请检查你的网络", Toast.LENGTH_SHORT).show();
                } else {
                    mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                    OkUtilCallback callback2 = new OkUtilCallback(mEmptyLayout) {
                        @Override
                        public void onMyResponse(String response, int id) {
                            Gson gson = new Gson();
                            Entity entity = gson.fromJson(response, Entity.class);
                            if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult())) && entity.getLogin()) {
                                BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                            } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                                finish();
                            } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(entity.getResult()))) {
                                BaseApplication.getIntstance().showToast(entity.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                                finish();
                            }
                        }
                    };
                    /*设置支付密码
                    userName string[Y] 邮箱
                    pwd string[Y] 支付密码
                    confirmPwd string[Y] 确认支付密码
                    type  [Y] 1:登录密码 2:支付密码*/
                    if (!TextUtils.isEmpty(name)){
                        userName = name;
                    }else if (!TextUtils.isEmpty(phone)){
                        userName = phone;
                    }else {
                        userName = email;
                    }
                    Log.e("1111",userName+"--"+pwd+"--"+psw);
                    OKUtilAPI.post(OKUtilAPI.settingPwd, "userName=" + userName + "&pwd=" + pwd+"&confirmPwd="+psw+"&type="+2, callback2);
                    Log.e("111-----",OKUtilAPI.settingPwd + "?userName=" + userName + "&pwd=" + pwd+"&confirmPwd="+psw+"&type="+2);
                }
            }
        });
    }

    @Override
    public void initData() {
        userId = SPUtils.getStringData(this, SPUtils.USERID);
        if (!NetUtils.isConnected(SettingPwdAgainActivity.this)) {
            Toast.makeText(SettingPwdAgainActivity.this, "请检查你的网络", Toast.LENGTH_SHORT).show();
        } else {
            mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
            callback1 = new OkUtilCallback(mEmptyLayout) {
                @Override
                public void onMyResponse(String response, int id) {
                    Gson gson = new Gson();
                    userData = gson.fromJson(response, UserData.class);
                    if ((!AppConfig.RESULT_OK.equalsIgnoreCase(userData.getResult()))) {
                        mEmptyLayout.setErrorType(EmptyLayout.NO_LOGIN);
                    } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(userData.getResult()))) {
                        BaseApplication.getIntstance().showToast(userData.getResult(), Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                    } else {
                        if (userData.getUser() != null) {
                            email = userData.getUser().getEmail();
                            phone = userData.getUser().getMobile();
                            name = userData.getUser().getUserName();
                        }
                    }
                }
            };
        }
        //请求会员资料
        OKUtilAPI.post(OKUtilAPI.getUser, "userId=" + userId, callback1);
        pwd = getIntent().getStringExtra("pwd");
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_setting_pwd_again;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }
}
